package metrobank;

import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.codehaus.xfire.MessageContext;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.handler.AbstractHandler;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.transport.http.EasySSLProtocolSocketFactory;
import org.jdom.Element;
import org.jdom.Namespace;
import org.tempuri.Echo;
import org.tempuri.EchoResponse;
import org.tempuri.SaleResponse;
import org.tempuri.SaleWithLineItemsResponse;
import org.tempuri.TCserviceClient;
import org.tempuri.TCserviceSoap;
import org.tempuri.VoidResponse;

public class MetroBankTest {

    static {
        ProtocolSocketFactory easy = new EasySSLProtocolSocketFactory();
        Protocol protocol = new Protocol("https", easy, 443);
        Protocol.registerProtocol("https", protocol);
    }

    public void test_orig_echo(String[] args) throws Exception {
        

    	TCserviceClient client=new TCserviceClient();
    	TCserviceSoap service=client.getTCserviceSoap();
        Echo echo=new Echo();
        echo.setMerchant("123");
        echo.setTerminal("123");
        echo.setTracking("123");
        
        EchoResponse  res=service.echo(echo);
        
        System.out.println(res.getEchoResult());
    }
    
    public void test_orig_void(String[] args) throws Exception {
        

    	TCserviceClient client=new TCserviceClient();
    	TCserviceSoap service=client.getTCserviceSoap();
    	org.tempuri.Void v=new org.tempuri.Void();
        
        
    	VoidResponse  res=service.voidTransaction(v);
        
        System.out.println(res.getVoidResult());
    }
    
    
    public void test_orig_sale(String[] args) throws Exception {
        

    	TCserviceClient client=new TCserviceClient();
    	TCserviceSoap service=client.getTCserviceSoap();
    	org.tempuri.Sale v=new org.tempuri.Sale();
        
        v.setMerchant("123");
        v.setTerminal("1234");
    	/*
        SaleResponse  res=service.sale(SaleWithLineItems)
        System.out.println(res.getSaleResult());
        */
    }
    
    public void test_orig_saleline(String[] args) throws Exception {
        

    	TCserviceClient client=new TCserviceClient();
    	TCserviceSoap service=client.getTCserviceSoap();
    	org.tempuri.SaleWithLineItems v=new org.tempuri.SaleWithLineItems();
        
        v.setMerchant("123");
        v.setTerminal("1234");
        SaleWithLineItemsResponse  res=service.sale(v);
        
        System.out.println(res.getSaleWithLineItemsResult());
    }
    
    public void test_sim(String[] args) throws Exception {
        
        
        

        String url = "https://gateway.merchantprocess.net/wscommerce.asmx";

        Service serviceModel = new ObjectServiceFactory().create(TCserviceSoap.class);
        TCserviceSoap service = (TCserviceSoap) new XFireProxyFactory().create( serviceModel, url);
        
        Echo echo=new Echo();
        echo.setMerchant("123");
        echo.setTerminal("123");
        echo.setTracking("123");
        
        EchoResponse  res=service.echo(echo);
        
        System.out.println(res.getEchoResult());
    }

    private class VendorIdHandler extends AbstractHandler {
        private String vendorId;

        VendorIdHandler(String vendorId) {
            super();
            this.vendorId = vendorId;
        }

        public String getVendorId() {
            return vendorId;
        }

        public void setVendorId(String vendorId) {
            this.vendorId = vendorId;
        }

        public void invoke(MessageContext context) throws Exception {
            final Namespace ns = Namespace.getNamespace("urn:commerce:vendor");
            Element header = context.getOutMessage().getHeader();

            if (header == null) {
                header = new Element("header");
                context.getOutMessage().setHeader(header);
            }

            Element vendor = new Element("VendorToken", ns);
            Element vendor_el = new Element("VendorId", ns);
            vendor_el.addContent(vendorId);
            vendor.addContent(vendor_el);
            header.addContent(vendor);
        }
    }

    private class UserPassHandler extends AbstractHandler {
        private String user;
        private String pass;

        UserPassHandler(String user, String pass) {
            super();
            this.user = user;
            this.pass = pass;
        }

        public void invoke(MessageContext context) throws Exception {
            final Namespace ns = Namespace.getNamespace("urn:commerce:authentication");
            Element header = context.getOutMessage().getHeader();

            if (header == null) {
                header = new Element("header");
                context.getOutMessage().setHeader(header);
            }

            Element root = new Element("AuthenticationToken", ns);
            Element username_el = new Element("Username", ns);
            Element password_el = new Element("Password", ns);

            username_el.addContent(user);
            password_el.addContent(pass);
            root.addContent(username_el);
            root.addContent(password_el);

            header.addContent(root);
        }
    }

    public static void main(String[] args) throws Exception {
        MetroBankTest cst=new MetroBankTest();
        cst.test_orig_sale(args);
        //cst.test_orig_saleline(args);
        //cst.test_orig_echo(args);
        //cst.test_orig_void(args);
        //cst.test_sim(args);
    }
}
